<template>
<div class="main_box" style="padding:10px;">
  <div class="add_wrap">
    <div class="tit">
      <h2>基础信息</h2>
      <el-button size="small" plain type="success" icon="fa fa-mail-forward" @click="returnList"> 返 回</el-button>
    </div>
    <el-form :model="dataForm" ref="dataForm" label-width="200px" :rules="roleRules">
      <el-row>
        <div style="padding: 2px 0 ;">
          <el-button type="primary" @click="addDataList" plain size="mini">新增器材</el-button>
        </div>
        <el-table :data="dataList" width="300px" :fit="true" highlight-current-row>
          <el-table-column prop="name" label="消毒器材">
            <template slot-scope="scope">
              <el-select v-model="scope.row.name" placeholder="请选择消毒器材">
                <el-option
                  v-for="item in nameList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column prop="numbers" label="数量">
            <template slot-scope="scope">
              <el-input v-model="scope.row.numbers"></el-input>
            </template>
          </el-table-column>
        </el-table>
      </el-row>

      <el-row>
        <el-form-item label="期望完成时间" prop="completeDate">
          <el-date-picker v-model="dataForm.completeDate" type="datetime"
                          :picker-options="pickerBeginDateBefore"
                          :default-value="timeDefaultShow"
                          value-format="yyyy-MM-dd hh:mm:ss"></el-date-picker>
        </el-form-item>

        <el-form-item label="消毒中心" prop="institutionId">
          <el-select
            v-model="dataForm.institutionId"
            clearable
            filterable
            reserve-keyword
            :loading="loadingIns"
            placeholder="请选择消毒中心"
          >
            <el-option
              v-for="item in pickInstitutionList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-row>

      <el-row>
        <el-form-item>
          <el-button plain type="primary" @click="sureClick">保 存</el-button>
          <el-button @click="returnList">取 消</el-button>
        </el-form-item>
      </el-row>
    </el-form>
  </div>
 </div>
</template>

<script>
import * as message from "@/constants/message";
import Region from "@/components/Region";
import DisinfectApi from "@/api/disinfect/disinfect";
import InstitutionApi from "@/api/institution/institution";
import AuthApi from "@/api/system/auth";
import EquipApi from "@/api/equip/equip";
export default{
  data(){
      return {
        dataForm:{
          disinfectId: "",
          completeDate: "",
          content: "",
          syUserId: ""
        },
        dataList:[
          {name:"",numbers:''}
        ],
        pickInstitutionList:[],
        pickerBeginDateBefore:{
          disabledDate(time) {
            return time.getTime() < Date.now();
          }
        },
        nameList:[
          { label: '穿刺针', value: '穿刺针' },
          { label: '压舌板', value: '压舌板' },
          { label: '舌钳', value: '舌钳' },
          { label: '开口器', value: '开口器' },
          { label: '引流管', value: '引流管' },
          { label: '体温计', value: '体温计' },
          { label: '胃肠减压器', value: '胃肠减压器' },
          { label: '砂锯', value: '砂锯' },
          { label: '弯盘', value: '弯盘' },
          { label: '治疗盘', value: '治疗盘' },
          { label: '耳镜', value: '耳镜' },
          { label: '额镜', value: '额镜' },
          { label: '咽喉镜', value: '咽喉镜' },
          { label: '听诊器', value: '听诊器' }
        ],
        loadingIns:false,
        syUserId: null,
        timeDefaultShow: '',
        syUserList: [],
        temperatureList: [],
        roleRules: {
          institutionId:[
            {  required: true, message: "请选择消毒中心", trigger: "change" }
          ],
          /*boxName: [
            { required: true, message: "请输入标本箱名称", trigger: "blur" },
            { max: 30, message: '长度不超过30个字', trigger: 'blur' }
          ],
          boxCode: [
            { required: true, message: "请输入标本箱编号", trigger: "blur" },
            { max: 20, message: '长度不超过20个字', trigger: 'blur' }
          ],
          areaIds:[
            {  required: true, message: "请选择所在区", trigger: "change" }
          ],
          hospitalAddress: [
            { required: true, message: "请输入详细地址", trigger: "blur" }
          ]*/
        }
      }
  },
  components: {
    Region
  },
  methods: {
    init(){
      this.listInstitution()
      if (this.$route.query.id) {
        this.dataForm.disinfectId = this.$route.query.id
        this.getData()
      }
    },

    listInstitution(){
      let _this = this
      _this.loadingIns = true
      InstitutionApi.selectInsByUser({})
        .then(res => {
          setTimeout(() => {
            _this.loadingIns = false
            _this.pickInstitutionList = res.data.map(item => {
              return {
                value: item.institutionId,
                label: item.institutionName
              }
            })
          }, 200)
        })
        .catch(error => {
          _this.pickInstitutionList = []
          console.error(error)
        })
    },
    addDataList(){
      this.dataList.push({name:"",numbers:''})
    },
    sureClick() {
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          this.$confirm(message.MSG_CONFIRM_SAVE_ALTER, message.TITLE_PROMPT, {})
            .then(() => {
              if (this.dataForm.disinfectId){
                this.editData();
              }else{
                this.addData();
              }
          });
        }
      })
    },
    addData() {
     let param = {
          institutionId: this.dataForm.institutionId,
          completeDate: this.dataForm.completeDate
      };
      if (this.dataList != null && this.dataList.length > 0){
        let content = ""
        this.dataList.forEach((item, index)=>{
          content = content + item.name + "[" + item.numbers +"]个" +","
        });
        if (content.indexOf(",") > 0){
          content = content.substr(0,content.length-1)
        }
        param.content = content
      }
      DisinfectApi.addDisinfect(param)
      .then(data => {
        this.$Utils.notifySuccess()
        this.returnList();
      })
    },
    editData() {
      let param = {
        esId: this.dataForm.esId,
        boxName: this.dataForm.boxName,
        boxCode: this.dataForm.boxCode,
        syUserId: this.dataForm.syUserId,
        etId: this.dataForm.etId,
        boxType: this.dataForm.boxType
      };
      if ((param.syUserId == null || param.syUserId == "") && param.etId){
        this.$message.error('请选择收样员');
      }
      if ((param.etId == null || param.etId == "") && param.syUserId){
        this.$message.error('请选择温控设备');
      }
      DisinfectApi.editDisinfect(param)
        .then(data => {
          this.$Utils.notifySuccess()
          this.returnList();
        })
    },
    getData () {
      DisinfectApi.getDisinfectInfo({
        disinfectId: this.dataForm.disinfectId
      }).then(res => {
        this.$Utils.bindingBean(this.dataForm, res.data)
        this.dataForm.syUserId = parseInt(res.data.syUserId);
        this.getSyUserList(this.dataForm.syUserId);
      })
    },
    getSyUserList (syUserId) {
      AuthApi.listAdminByRole({
        roleId: 4,syUserId:syUserId
      }).then(res => {
        this.syUserList = res.data
      })
    },
    getTemperatureList () {
      let _this = this
      EquipApi.getTempForSelect({
        esId: this.dataForm.esId
      }).then(res => {
        setTimeout(() => {
          _this.loadingIns = false
          _this.temperatureList = res.data.map(item => {
            return {
              value: item.etId,
              label: item.equipName
            }
          })
        }, 200)
      })
    },
    returnList(){
      this.$store.dispatch('delView', this.$route);
      this.$router.push({path: '/disinfect/disinfectManagement'});
    }
  },
  mounted(){
    this.timeDefaultShow = new Date()
    this.init()
  }
}
</script>
<style lang="scss" src="@/assets/css/views/add.scss" scope>
</style>
<style scoped>
.add_wrap{
  padding-top: 10px;
  padding-left: 23px;
}
</style>
